package com.gxa.controller.points;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.pointsdto.ExchangeRecordDto;
import com.gxa.entity.community.UserList;
import com.gxa.entity.points.ExchangeRecord;
import com.gxa.entity.points.Gift;
import com.gxa.service.points.ExchangeRecordService;
import com.gxa.service.points.GiftService;
import com.gxa.service.points.IntegrationService;
import com.gxa.vo.ExchangeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */
@Api(tags = "礼品兑换模块")
@RestController
@RequestMapping("/exchange-record")
public class ExchangeRecordController {
    @Autowired
    private ExchangeRecordService exchangeRecordService;

    @Autowired
    private GiftService giftService;

    @ApiOperation(value = "礼品列表接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/giftList")
    public Result giftList(){
        try{
            List<Gift> giftList = this.giftService.queryList();
            return Result.success(giftList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

    @ApiOperation(value = "增加兑换记录接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/add")
    public Result add(@RequestBody ExchangeRecordDto exchangeRecordDto){
          boolean a =this.exchangeRecordService.add(exchangeRecordDto);
          if (a) {
              return Result.success("添加成功");
          }else {
          return Result.failed("添加失败");
      }
    }


    @ApiOperation(value = "根据id查找兑换记录接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/exchangeRecord/{id}")
    public Result queryByid(@ApiParam(value = "输入要查找的id") @PathVariable("id") Integer id){
        try {
            ExchangeRecordDto exchangeRecordDto = this.exchangeRecordService.queryById(id);
            return  Result.success(exchangeRecordDto);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("没有数据");
        }
    }

    @ApiOperation(value = "领取按钮接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PutMapping("/get/{id}")
    public Result edit(@PathVariable("id") Integer id ){
        Date date = new Date();
        try {
            this.exchangeRecordService.updateById(id,date);
            return Result.success("领取成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("领取失败");
        }
    }


    @ApiOperation(value = "退回积分接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @DeleteMapping("/exchangeRecord")
    public Result delete(@RequestParam("id") Integer id,@RequestParam("userNumber") String userNumber, @RequestParam("score") Integer
    score){
        System.out.println(userNumber);
        System.out.println(score);
        try {
            this.exchangeRecordService.delete(id, userNumber, score);
            return Result.success("退回成功");
        }catch (Exception e) {
            return Result.failed("退回失败");
        }
    }


    @ApiOperation(value = "查找兑换记录接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/queryAll")
    public Result<PageInfo> queryAll(@RequestParam("pageNum") Integer pageNum,@RequestParam("count") Integer count) {
        try{
            PageInfo<ExchangeRecordDto> exchangeRecordDtos = this.exchangeRecordService.queryAll(pageNum, count);
            return Result.success(exchangeRecordDtos);
        }catch (Exception e){
            return Result.failed("查找失败");
        }
    }

    @ApiOperation(value = "条件查找兑换记录接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/queryByVo")
    public Result<PageInfo> queryByVo(@RequestBody ExchangeVo exchangeVo) {
        System.out.println(exchangeVo);
        try{
            PageInfo<ExchangeRecordDto> exchangeRecordDtos = this.exchangeRecordService.queryByVo(exchangeVo);
            return Result.success(exchangeRecordDtos);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查找失败");
        }
    }
}
